package Servlets;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

import entitys.CustomerInfo;

import Util.EntityManagerAdpater;

public class HelloWorldServlet extends HttpServlet {

	private static final long serialVersionUID = 1031422249396784970L;
	static Logger log = Logger.getLogger(HelloWorldServlet.class);

	public void doGet(HttpServletRequest req, HttpServletResponse resp) {

		// persistExmaple();

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		log.info("Receive doPost..");

	}

	private void persistExmaple() {
		log.info("Receive doGet..");
		EntityManagerAdpater em = new EntityManagerAdpater();
		EntityManager emf = em.getEntityManagerFactory().createEntityManager();
		try {
			CustomerInfo customerInfo = new CustomerInfo();
			customerInfo.setAge(1);
			customerInfo.setFirstName("test");
			customerInfo.setLastName("asd");
			em.persist(customerInfo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(e);
		} finally {

			if (emf != null && emf.isOpen())
				if (emf.getTransaction().isActive()) {
					emf.flush();
				}
			emf.clear();
			emf.close();
		}
	}

}
